import 'package:basic_modle/upload_data.dart';
import 'package:basic_network/http_data.dart';
import 'package:dio/dio.dart';
import 'http_request.dart';

// http请求静态类
class Http {
  /// 初始化公共属性 如果需要覆盖原配置项 就调用它
  ///
  /// [baseUrl] 地址前缀
  /// [connectTimeout] 连接超时赶时间
  /// [receiveTimeout] 接收超时赶时间
  /// [headers] 请求头
  /// [interceptors] 基础拦截器
  static void init({
    String? baseUrl,
    int? connectTimeout,
    int? receiveTimeout,
    Map<String, dynamic>? headers,
    List<Interceptor>? interceptors,
  }) {
    HttpRequest().init(
      // baseUrl: baseUrl,
      connectTimeout: connectTimeout,
      receiveTimeout: receiveTimeout,
      headers: headers,
      interceptors: interceptors,
    );
  }

  /// 设置请求头
  static void setHeaders(Map<String, dynamic> headers) {
    HttpRequest().setHeaders(headers);
  }

  /// 取消请求
  static void cancelRequests({CancelToken? token}) {
    HttpRequest().cancelRequests(token: token);
  }

  /// restful get 操作
  static Future get(
    String? path, {
    Map<String, dynamic>? params,
    Options? options,
    CancelToken? cancelToken,
  }) async {
    return await HttpRequest().get(
      path,
      params: params,
      options: options,
      cancelToken: cancelToken,
    );
  }

  /// restful post 操作
  /// urlType 请求地址 1 阜外 2 是soa
  static Future post(
    String path, {
    Map<String, dynamic>? params,
    String? baseUrl,
    dynamic? data,
    Options? options,
    CancelToken? cancelToken,
    bool isShowLoading = false,
    String loadingTitle = '加载中...',
  }) async {
    return await HttpRequest().post(path,
        baseUrl: baseUrl,
        params: params,
        data: data,
        options: options,
        cancelToken: cancelToken,
        isshowLoading: isShowLoading,
        loadingTitle: loadingTitle);
  }

  static Future postMap(
    String path, {
    Map<String, dynamic>? params,
    String? baseUrl,
    dynamic? data,
    Options? options,
    CancelToken? cancelToken,
    bool isShowLoading = false,
    String loadingTitle = '加载中...',
  }) async {
    return await HttpRequest().postMap(path,
        baseUrl: baseUrl,
        params: params,
        data: data,
        options: options,
        cancelToken: cancelToken,
        isshowLoading: isShowLoading,
        loadingTitle: loadingTitle);
  }

  /// restful post form 表单提交操作
  static Future<UploadData> postForm(String? path,
      {Map<String, dynamic>? data,
      Options? options,
      CancelToken? cancelToken,
      bool isShowLoading = false,
      String baseUrl = "",
      String loadingTitle = '加载中...'}) {
    return HttpRequest().postForm(path,
        data: data,
        options: options,
        cancelToken: cancelToken,
        baseUrl: baseUrl);
  }
}
